//
//  HomeTableViewController.swift
//  weibo1
//
//  Created by Apple on 15/11/26.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking


private let Homecell = "cell"
class HomeTableViewController: BaseTableViewController {
    //添加数组属性
    private lazy var statuse = [statuses]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !userlogin{
             loginView?.setWithInfo("关注一些人，回这里看看有什么惊喜", imageName: nil)
            return
        
        }
       
        
        //注册cell
        prepareTableView()
        
        loadData { (status) -> () in
            guard let list = status else {
                
                //数据为空
                SVProgressHUD.showErrorWithStatus("网络不好")
                return
            }
            //记录数据
            self.statuse = list
        
            //刷新列表
            self.tableView.reloadData()
        }
        
        
    }
    //注册cell
    private func prepareTableView(){
        tableView.registerClass(StatusesCell.self, forCellReuseIdentifier: Homecell)
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        //取消分割线的
        tableView.separatorStyle = .None
    }
    
    
    
    //MARK:加载数据
    private func loadData(finished: (status :[statuses]?) -> ()) {
        
        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let manager = AFHTTPSessionManager()
        //给token赋值
        guard let token = UserAccountViewModel().token else{
            
            SVProgressHUD.showErrorWithStatus("没有登录")
            //回调
            finished(status: nil)
            return
        }
        
        let parameters = ["access_token" : token]
        manager.GET(url, parameters: parameters, success: { (_, result) -> Void in
            guard let arrays = result["statuses"] as? [[String : AnyObject]] else {
                print("类型有吴")
                return
            }
            //            实例化数组
            var last = [statuses]()
            for itme in arrays {
                
                let s = statuses(dict: itme)
               
                last.append(s)
                
            }
//            print(arrays)
            finished(status: last)
            
            }) { (_, error) -> Void in
                
                print(error)
        }
        
    }
    //MARK:数据源方法
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuse.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(Homecell, forIndexPath: indexPath) as! StatusesCell
        //赋值
        
//        cell.textLabel?.text = statuse[indexPath.row].user?.name
        cell.status = statuse[indexPath.row]
        return cell
        
    }
    
}
